<?php

namespace backend\controllers;


use common\models\Active;
use common\models\Order;
use common\models\User;
use common\models\Menu;
use common\service\CaptchaService;
use common\service\ConstantService;
use common\service\GlobalUrlService;
use common\service\MenuService;

class IndexController extends BaseController
{
    public function actionIndex()
    {
        $this->layout = 'main';
        return $this->render('index');
    }

    /**
     * 获取菜单
     * @return mixed
     */
    public function actionGetMenu(){
        $menus = Menu::find()->where(['is_show'=>1])->orderBy(['sort'=>SORT_ASC])->asArray()->all();
        $menus = MenuService::getMenus($menus);
        $data['contentManagement'] = $menus;
        return $this->renderJson($data,'',ConstantService::$response_success);
    }

    /**
     * 后台首页数据
     * @return string
     */
    public function actionDashboard(){
        $start_time = strtotime(date('Y-m-d ') . '00:00:00');
        $end_time = strtotime(date('Y-m-d ') . '23:59:59');
        //需要统计的数据
        //1 日,总订单数  已支付
        $order_query = Order::find();
        $max_time = ['>=', 'created_time', $start_time];
        $min_time = ['<=', 'created_time', $end_time];
        $pay_status = ['=', 'pay_status', 1];
        $noeq = ['!=', 'order_status', 6];
        $day_order = $order_query->select('count(id) as day_order_count')
            ->andWhere($max_time)
            ->andWhere($min_time)
            ->andWhere($pay_status)
            ->andWhere(['>=', 'order_status', 1])
            ->asArray()
            ->one();
        $count_order = Order::find()->select('count(id) as order_count')
            ->where(['pay_status' => 1])
            ->andWhere(['>=', 'order_status', 1])
            ->asArray()
            ->one();

        //2 日新增用户数,用户总数 x
        $user_query = User::find();
        $user_query->andWhere(['>=', 'create_time', $start_time]);
        $user_query->andWhere(['<=', 'create_time', $end_time]);
        $day_user = $user_query->select(['count(id) as day_user_count'])->asArray()->one();
        $count_user = User::find()->select(['count(id) as user_count'])->asArray()->one();
        //3. 日,总商品销量
        $day_goods = $order_query
            ->select('sum(goods_num) as day_goods_count')
            ->andWhere($noeq)
            ->andWhere($max_time)
            ->andWhere($min_time)
            ->andWhere($pay_status)
            ->asArray()
            ->one();
        $count_goods = Order::find()
            ->select('sum(goods_num) as goods_count')
            ->andWhere($noeq)
            ->andWhere($pay_status)
            ->asArray()
            ->one();

        //4. 日活跃用户数(可选周期) 这个暂时无
        //5. 日新增,总拼团数
        $activity_query = Active::find();
        $activity_query->andWhere(['>=','end_time',$end_time]);
        $activity_query->andWhere(['is_del'=>'0']);
        $activity_query ->andWhere(['status'=>'1']);
        $day_active = $activity_query->select('count(id) as day_active_count')
            ->asArray()
            ->one();
        $count_active = Active::find()
            ->select('count(id) as active_count')
            ->asArray()->one();

        //6 日,总销售金额
        $day_money = $order_query
            ->select('sum(order_money) as day_money_count')
            ->andWhere($max_time)
            ->andWhere($min_time)
            ->andWhere($noeq)
            ->andWhere($pay_status)
            ->asArray()
            ->one();
        $count_money = Order::find()
            ->select('sum(order_money) as money_count')
            ->andWhere($pay_status)
            ->andWhere($noeq)
            ->asArray()
            ->one();

        $data = [
            'day_user' => $day_user['day_user_count'],
            'count_user' => $count_user['user_count'],
            'day_order' => $day_order['day_order_count'],
            'count_order' => $count_order['order_count'],
            'day_goods' => $day_goods['day_goods_count'],
            'count_goods' => $count_goods['goods_count'],
            'day_money' =>sprintf("%.2f",$day_money['day_money_count']),
            'count_money' =>sprintf("%.2f",$count_money['money_count']),
            'day_active' =>$day_active['day_active_count'],
            'count_active' =>$count_active['active_count'],
        ];

        return $this->render('dashboard',[
            'data'=>$data
        ]);
    }

    /**
     * 获取图片验证码
     */
    public function actionGetCaptcha(){
        $path = \Yii::$app->getBasePath().'/web/font/captcha.ttf';
        $captcha = new CaptchaService($path);
        $captcha->createCode();
        //设置缓存
        $this->addCookie($this->cookie_captcha_name,$captcha->getCode(),120);
        //生成图片
        $captcha->doimg();
    }
}